#include "SDL.h"
#include "SDL_image/SDL_image.h"
#include <string>
#include <stdlib.h>
#include "SDL_graphics.h"
#include "Enigma.h"

using namespace std;

#ifdef _WIN32
const string resourcePath = "C:\\Users\\ryan\\Documents\\C++ Projects\\Enigma Physics Engine SVN\\Test code\\Resources\\";
#else
const string resourcePath = "/Users/Darryl/Desktop/Programming/Enigma Physics Engine/Test code/resources/";
#endif

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_DEPTH = 32;

const int NUM_PLANETS = 2;

const float PIXELS_TO_METERS = 427e6;
const float METERS_TO_PIXELS = 1 / PIXELS_TO_METERS;
const int CENTER_X = SCREEN_WIDTH / 2;
const int CENTER_Y = SCREEN_HEIGHT / 2;
const int TIME_STEP = 3600;

SDL_Surface* screen;
SDL_Surface* star;

SDL_Event event;
bool gameOn = true;

Singularity* planets[NUM_PLANETS];

int main(int argc, char* argv[])
{
	int i, j;
	Uint32 WHITE;
	bool mouseIsDown = false;	
	SDL_Init(SDL_INIT_EVERYTHING);
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);
	SDL_WM_SetCaption("Enigma Physics Engine Test", NULL);
	SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format,0,0,0));
	WHITE = SDL_MapRGB(screen->format, 255,255,255);
	
	// Create the planets
	planets[0] = new Singularity(1.9891e30, 6.955e8, 6.67300e-11);	// Sun
	planets[1] = new Singularity(5.9736e24, 6371.0e3, 6.67300e-11);	// Earth
	planets[0]->global = true;
	planets[1]->global = true;

	// Set starting state of earth, y is 0 by default
	planets[1]->pos.x = 149.5e9;
	planets[1]->vel.y = 29783;
	
	while (gameOn)
	{
		while (SDL_PollEvent(&event))
		{
			switch(event.type)
			{
				case SDL_QUIT:
					gameOn = false;
					break;
			}
		}

		for (i = 0; i < NUM_PLANETS; i++)
		{
			for (j = i + 1; j < NUM_PLANETS; j++)
			{
					planets[1]->affect(planets[0]);
					planets[0]->affect(planets[1]);
			}
		}

		for (i = 0; i < NUM_PLANETS; i++)
		{
			planets[i]->update(TIME_STEP);
			planets[i]->accel.x = 0;
			planets[i]->accel.y = 0;
		}

		SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format,0,0,0));
		if( SDL_MUSTLOCK(screen))
			SDL_LockSurface(screen);
		for (i = 0; i < NUM_PLANETS; i++)
			putpixel32(screen, (planets[i]->pos.x * METERS_TO_PIXELS) + CENTER_X, (planets[i]->pos.y * METERS_TO_PIXELS) + CENTER_Y, WHITE);
		if( SDL_MUSTLOCK(screen))
			SDL_UnlockSurface(screen);
		
		SDL_Flip(screen);	
	}
	
	
	SDL_FreeSurface(star);
	SDL_Quit();
	return 0;
}






